package com.fsy.weibao.workorder.flow;

import com.fsy.weibao.workorder.domain.WorkOrder;
import com.fsy.weibao.workorder.service.WorkOrderService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class WorkOrderFlowListener implements TaskListener {

    public static ApplicationContext applicationContext;

    @Override
    public void notify(DelegateTask delegateTask) {
        WorkOrderService workOrderService = applicationContext.getBean(WorkOrderService.class);
        String procId = delegateTask.getProcessInstanceId();
        WorkOrder workOrder = workOrderService.getWorkOrderByProcess(procId);
        if( workOrder == null ) {
            log.warn("No work order with processId={}", procId);
            return;
        }

//        String taskKey = delegateTask.getTaskDefinitionKey();
//        switch(taskKey) {
//            case "dispatchTask":
//                workOrder.setStatus(WorkOrder.STATUS_DISTRIBUTE);
//                break;
//            case "grabTask":
//                workOrder.setStatus(WorkOrder.STATUS_GRAB);
//                break;
//            case "preHandleTask":
//                workOrder.setStatus(WorkOrder.STATUS_PRE_HANDLE);
//                break;
//            case "handlingTask":
//                workOrder.setStatus(WorkOrder.STATUS_HANDLING);
//                break;
//            case "approveTask":
//                workOrder.setStatus(WorkOrder.STATUS_APPROVAL);
//                break;
//        }
//        workOrderService.update(workOrder);
    }
}
